/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(!head)
            return nullptr;
        struct ListNode* fast = head;
        struct ListNode* slow = head;
        while(fast && fast->next){
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow){
                while(head != slow){       
                    head = head->next;
                    slow = slow->next;
                }
                return head;  
            }
        }
        return nullptr;
        //注意：快慢指针要从同一出发点出发
    }
};
